package com.tanhua.server.controller;


import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserService userService;


    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if(userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }
    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo ,@RequestHeader("Authorization") String token,Long userID){

        //设置用户id
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 更新用户头像
     */
    @PostMapping("/header")
    public ResponseEntity updateUserHead(MultipartFile headPhoto,
                                         @RequestHeader("Authorization") String token) throws IOException {
        userInfoService.updateHead(headPhoto,UserHolder.getUserId());
        return ResponseEntity.ok(null);
    }

    /**
     * 发送验证码 --修改
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendMsg(){
        userService.sendMsg(UserHolder.getMobile());
        return ResponseEntity.ok(null);
    }

    /**
     * 校验验证码 --修改
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkCode(@RequestBody Map map){
        String code= (String) map.get("verificationCode");
        Map checkMap= userService.checkCode(UserHolder.getMobile(),code);
        return ResponseEntity.ok(checkMap);
    }

    /**
     * 保存手机号
     */
    @PostMapping("/phone")
    public ResponseEntity saveNewPhone(@RequestBody Map map){
        String phone =(String) map.get("phone");
         userService.updatePhone(phone);
        return ResponseEntity.ok(null);
    }

    //互相喜欢，喜欢，粉丝的数量
    @GetMapping("/counts")
    public ResponseEntity counts(){
        Map map = userService.counts();
        return ResponseEntity.ok(map);
    }

    /**
     * 查询"谁看过我"访客列表
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity lookedMeList(@PathVariable("type") String type ,@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int pagesize,String nickname){
        PageResult pr = userService.list(type,page,pagesize);
        return ResponseEntity.ok(pr);
    }

    @GetMapping("/{id}/alreadyLove")
    public ResponseEntity alreadyLove(){
        return ResponseEntity.ok(null);
    }

}
